<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="search-card">
      <el-form
        size="small"
        ref="queryParams"
        :model="queryParams"
        class="formBox"
        label-width="88px"
      >
        <el-row :gutter="0">
          <el-col :span="5">
            <el-form-item label="餐别名称" prop="mealType">
              <el-input
                v-model="queryParams.mealType"
                placeholder="请选择餐别名称"
                maxlength="50"
                show-word-limit
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="启用状态" prop="useStatus">
              <el-select
                v-model="queryParams.useStatus"
                placeholder="请选择启用状态"
                style="width: 100%"
              >
                <el-option value="1" label="启用" />
                <el-option value="2" label="停用" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item>
              <div class="dis aic">
                <div class="flex_1"></div>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                >
                  搜索
                </el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
                  重置
                </el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="search-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="buttonMethods(1)"
            v-hasPermi="['teamMeal:teamMeal:mealType:add']"
          >
            添加
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['teamMeal:teamMeal:mealType:export']"
          >
            导出
          </el-button>
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-card>
    <el-card class="table-card">
      <el-table :data="dataList" stripe border>
        <!-- <el-table-column label="校区" align="center" prop="campusName" /> -->
        <el-table-column label="餐别名称" align="center" prop="mealType" />
        <el-table-column label="用餐时间" align="center" prop="mealTime" />
        <el-table-column label="提前几个小时请假" align="center" prop="leaveTime" />
        <el-table-column label="启用状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.useStatus == 1">启用</el-tag>
            <el-tag v-if="scope.row.useStatus == 2" type="danger">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="使用状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isUse == 1">使用中</el-tag>
            <el-tag v-if="scope.row.isUse == 0" type="danger">未使用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="创建人" align="center" prop="createName" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
        <el-table-column label="编辑人" align="center" prop="updateName" />
        <el-table-column label="编辑时间" align="center" prop="updateTime" width="160" />
        <el-table-column label="操作" align="center" width="200px">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              :disabled="scope.row.isUse == 1 ? true : false"
              @click="buttonMethods(1, scope.row)"
              v-hasPermi="['teamMeal:teamMeal:mealType:edit']"
              >修改</el-button
            >
            <el-button
              type="text"
              size="mini"
              :disabled="scope.row.isUse == 1 ? true : false"
              @click="buttonMethods(4, scope.row)"
              v-hasPermi="['teamMeal:teamMeal:mealType:remove']"
              >删除
            </el-button>
            <el-button
              type="text"
              size="mini"
              :disabled="scope.row.isUse == 1 ? true : false"
              @click="buttonMethods(5, scope.row)"
              v-hasPermi="['teamMeal:teamMeal:mealType:status']"
            >
              <span v-if="scope.row.useStatus == 1">停用</span>
              <span v-if="scope.row.useStatus == 2">开启</span>
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="queryForm" label-width="160px">
        <el-row>
          <!-- <el-col :span="20">
            <el-form-item label="校区" class="is-required">
              <el-select
                v-model="queryForm.campusId"
                placeholder="请选择校区"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in campusList"
                  :key="index"
                  :value="item.id"
                  :label="item.campusName"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="20">
            <el-form-item label="餐食类型" class="is-required">
              <el-select
                v-model="queryForm.meals"
                placeholder="请选择餐食类型"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dict.type.meal_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="餐别名称" class="is-required">
              <el-input
                v-model="queryForm.mealType"
                placeholder="请输入餐别名称"
                show-word-limit
                maxlength="50"
              />
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="用餐时间" class="is-required">
              <el-time-select
                v-model="queryForm.mealTime"
                :picker-options="{
                  start: '05:00',
                  step: '00:15',
                  end: '22:00',
                }"
                placeholder="请选择用餐时间"
                class="width_100"
              >
              </el-time-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="提前几个小时请假" class="is-required">
              <el-input
                v-model="queryForm.leaveTime"
                placeholder="提前几个小时请假"
                @blur="itemPriceBlur"
              >
                <template slot="append">在就餐时间前几个小时允许家长请假</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="使用状态" class="is-required">
              <el-radio-group v-model="queryForm.useStatus">
                <el-radio :label="1">启用 </el-radio>
                <el-radio :label="2">停用 </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="备注">
              <el-input
                type="textarea"
                placeholder="请输入内容"
                v-model="queryForm.remark"
                maxlength="200"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="buttonMethods(2)">确 定</el-button>
        <el-button @click="buttonMethods(3)">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addSchoolMealType,
  updateSchoolMealType,
  deleteSchoolMealType,
  queryListSchoolMealTypePage,
} from "@/api/teamMeal/meal";
export default {
  name: "teamMealmealType",
  dicts: ["sys_open_status", "meal_status"],
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 1,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mealType: null,
        useStatus: null,
      },
      dataList: [],
      campusList: [],
      gradeList: [],
      queryForm: {
        meals: null,
        mealType: null,
        mealTime: null,
        leaveTime: null,
        useStatus: null,
        remark: null,
      },
    };
  },
  created() {
    this.getList();
  },
  activated() {
    this.$forceUpdate();
    this.getList();
  },
  methods: {
    // 列表
    getList() {
      queryListSchoolMealTypePage(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.dataList = res.rows;
          this.total = res.total;
        }
      });
    },
    itemPriceBlur() {
      var leaveTime = this.queryForm.leaveTime;
      this.queryForm.leaveTime = leaveTime.replace(/[^\d]/g, "");
    },
    /**
     * 按钮操作的方法
     *
     * @param options 操作选项，用于区分不同的按钮操作
     * @param value 按钮值
     */

    buttonMethods(options, value) {
      // 添加 修改
      var that = this;
      if (options == 1) {
        if (value) {
          this.title = "修改餐别";
          this.queryForm = { ...value };
          this.open = true;
        } else {
          this.title = "添加餐别";
          this.queryForm = {
            useStatus: 1,
          };
          this.open = true;
        }
      }
      // 提交
      else if (options == 2) {
        var data = { ...this.queryForm };
        console.log(data);
        // if (!data.campusId) {
        //   return this.$modal.msgError("请选择校区");
        // }
        if (!data.mealType) {
          return this.$modal.msgError("请选择餐别名称");
        }
        if (!data.mealTime) {
          return this.$modal.msgError("请选择用餐时间");
        }
        if (!data.leaveTime) {
          return this.$modal.msgError("请输入提前几个小时请假");
        }
        data.useStatus = data.useStatus + "";
        if (!data.useStatus) {
          return this.$modal.msgError("请选择是否启用");
        }
        // 修改餐别信息
        if (data.id) {
          updateSchoolMealType(data).then((res) => {
            if (res.code == 200) {
              that.getList();
              that.open = false;
              that.$modal.msgSuccess("修改成功");
            } else {
              that.$modal.msgError("修改失败");
            }
          });
        }
        // 添加餐别信息
        else {
          addSchoolMealType(data).then((res) => {
            if (res.code == 200) {
              that.getList();
              that.open = false;
              that.$modal.msgSuccess("添加成功");
            } else {
              that.$modal.msgError("添加失败");
            }
          });
        }
      }
      // 关闭弹框
      else if (options == 3) {
        this.title = "";
        this.queryForm = {
          useStatus: "1",
        };
        this.open = false;
      }
      // 删除
      else if (options == 4) {
        this.$confirm("此操作将永久删除餐别, 是否继续?", "删除", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deleteSchoolMealType({
              id: value.id,
              isUse: value.isUse,
              isDel: value.isDel == 0 ? 1 : 0,
            }).then((res) => {
              if (res.code == 200) {
                that.getList();
                that.$modal.msgSuccess("删除成功");
              } else {
                that.$modal.msgError("删除失败");
              }
            });
          })
          .catch(() => {});
      }
      // 关闭 开启
      else if (options == 5) {
        var str = value.useStatus == 1 ? "停用" : "开启";
        this.$confirm("此操作将" + str + "餐别, 是否继续?", str, {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            updateSchoolMealType({
              id: value.id,
              useStatus: value.useStatus == 1 ? 2 : 1,
            }).then((res) => {
              if (res.code == 200) {
                that.$modal.msgSuccess("操作成功");
                that.getList();
              } else {
                that.$modal.msgError("操作失败");
              }
            });
          })
          .catch(() => {});
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.handleQuery();
    },
    // 导出
    handleExport() {
      this.download(
        "/meal/exportSchoolMealType",
        {
          ...this.queryParams,
        },
        `餐别管理导出.xlsx`
      );
    },
  },
};
</script>
<style>
.padding_4_10 {
  padding: 4px 10px !important;
}
</style>
